<?php
class BannerController extends AdminAppController
{
    public $uses = array('Admin.Banner');
    public $paginate = array(
        'limit' => 25,
        'order' => array(
            'Banner.priority' => 'desc'
        ),
        'conditions' => array(
            'Banner.is_deleted' => 0
        )
    );

    public function index()
    {
        $this->Banner->contain();
        $data = $this->paginate('Banner');
        $this->set(compact('data'));
    }

    public function add()
    {
        if ($this->request->is('post')) {
            try {
                $this->Banner->saveBanner($this->request->data['Banner']);
                $this->redirect(array('action' => 'index'));
                $this->Session->setFlash(__('The Banner has been saved'));
            } catch (Exception $e) {
                $validationErrors = $this->Banner->validationErrors;
                $this->set(compact('validationErrors'));
                $this->Session->setFlash($e->getMessage());
            }
        }
    }

    public function edit($id=null)
    {
        $this->Banner->contain();
        $service = $this->Banner->findById($id);
        if ($this->request->is('get')) {
            $this->request->data = $service;
        } else {
            try {
                $isUpdate = true;
                $this->Banner->saveBanner($this->request->data['Banner'], $isUpdate, $service['Banner']);
                $this->redirect(array('action' => 'index'));
                $this->Session->setFlash(__('The Banner has been saved'));
            } catch (Exception $e) {
                $validationErrors = $this->Banner->validationErrors;
                $this->set(compact('validationErrors'));
                $this->Session->setFlash($e->getMessage());
            }
        }
    }

    public function delete($id=null)
    {
        if ($id == null) {
            $this->redirect('/');
        }
        $data = array(
            'Banner' => array(
                'id' => $id,
                'is_deleted' => 1
            )
        );
        $this->Banner->save($data);
        $this->redirect('/admin/banner');
    }

}
